package com.xpgk.xpsoft.department;

import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.common.controller.BaseController;
import com.xpgk.common.kit.NetKit;
import com.xpgk.common.kit.PubKit;
import com.xpgk.model.FrmUser;
import com.xpgk.xpsoft.code.CodeService;
import com.xpgk.xpsoft.common.XPSoftLog;
import com.xpgk.xpsoft.constants.XPSoftConstant;
import com.xpgk.xpsoft.user.UserService;

public class DepartmentController extends BaseController {

	
	public void index() {
		int CurUserID = getCurUserID();
		FrmUser user = UserService.getUser(CurUserID);
		if ("2".equals(user.getTYPE()))// 操作员不具备用户管理权限
		{
			renderError(404);
			return;
		}
		setAttr("bCanAdd", UserService.hasRight(CurUserID, "/xpDepartment/add_ui"));
		setAttr("bCanEdit", UserService.hasRight(CurUserID, "/xpDepartment/edit_ui"));
		render("index.html");
	}

	/**
	 * 分页数据
	 */
	public void pagingData() {
		int CurUserID = getCurUserID();
		FrmUser user = UserService.getUser(CurUserID);
		Kv cond = Kv.by("NAME like ", isParaBlank("name") ? null : getPara("name") + "%").set("BH like ", isParaBlank("bh") ? null : getPara("bh", "") + "%");
		String data = paging("xpSoftDepartment.findList", Kv.by("cond", cond).set("DEPARTMENTID", user.getDEPARTMENTID()));
		renderJson(data);
	}

	// 注：定义函数名时，如"void add()",页面的展示请以_ui结尾，void add_ui() 涉及到权限问题
	// add与add_ui有同样的权限
	public void add_ui() {
		setAttr("xzqhList", CodeService.getXZQHList());
		FrmUser user = UserService.getUser(getCurUserID());
		if (user == null)
			return;

		int deptID = PubKit.toInt(user.getDEPARTMENTID(), -1);
		String bh = DepartmentService.GetNextDeptBH(deptID);
		Record dept = new Record();
		dept.set("bh", bh);
		setAttr("dept", dept);
		render("add.html");
	}

	// 注册
	@Before(Tx.class)
	public void add() {
		Record dept = GetRecord("dept");
		dept.set("parentID", UserService.getUser(getCurUserID()).getInt("departmentID"));
		String result = DepartmentService.saveDept(dept);
		if (!result.equals("ok")) {
			renderJSONMsg(result, false);
			return;
		}
		XPSoftLog.write(getCurUserID(), "addDepartment", "dept=" + dept.toString(), XPSoftConstant.log_department, 1, "部门注册", NetKit.getClientIP(getRequest()));
		setAttr("ID", dept.getInt("id"));
		renderJSONMsg("添加成功.", true);
	}

	// 修改
	public void edit_ui() {
		int id = getParaToInt("ID", 0);
		setAttr("xzqhList", CodeService.getXZQHList());
		Record dept = Db.findById("FRM_DEPARTMENT", id);
		setAttr("dept", dept);
		setAttr("bSelf", id == UserService.getUser(getCurUserID()).getInt("departmentID"));
		setAttr("bEdit", 1);
		render("edit.html");
	}

	// 修改
	@Before(Tx.class)
	public void edit() {
		Record dept = GetRecord("dept");
		String result = DepartmentService.updateDept(dept);
		if (!result.equals("ok")) {
			renderJSONMsg(result, false);
			return;
		} // 2 改为“修改部门”
		XPSoftLog.write(getCurUserID(), "editDepartment", "dept=" + dept.toString(), XPSoftConstant.log_department, 2, "部门修改", NetKit.getClientIP(getRequest()));
		renderJSONMsg("修改成功.", true);
	}

	// 修改权限
	public void right_ui() {
		int id = getParaToInt("ID", 0);
		int bAdd = getParaToInt("bAdd", 0);
		setAttr("id", id);
		setAttr("bAdd", bAdd);
		setAttr("zNode", JsonKit.toJson(DepartmentService.getRightListForTree(id)));
		render("right.html");
	}

	// 修改部门管理员权限
	@Before(Tx.class)
	public void right() {
		int id = getParaToInt("ID", 0);
		String rights = getPara("rights", "");
		Integer[] result = null;
		if (!PubKit.isBlank(rights)) {
			String[] rights2 = rights.split(",");
			result = new Integer[rights2.length];
			for (int i = 0; i < result.length; i++)
				result[i] = Integer.parseInt(rights2[i]);
		}
		DepartmentService.updateDeptRights(id, result); // 4 修改部门管理员权限
		XPSoftLog.write(getCurUserID(), "editDepartmentRight", "rights=" + rights.toString(), XPSoftConstant.log_department, 4, "修改部门管理员权限", NetKit.getClientIP(getRequest()));
		renderJSONMsg("设置成功.", true);
	}

	@Before(Tx.class)
	public void editState() {
		int id = getParaToInt("ID", 0);
		int state = getParaToInt("state", -1);
		if (state != -1) {
			Record dept = Db.findById("FRM_DEPARTMENT", id);
			dept.set("state", state);
			String result = DepartmentService.updateDept(dept);
			if (!result.equals("ok")) {
				renderJSONMsg(result, false);
				return;
			}
			renderJSONMsg("修改成功.", true);
		} else {
			renderJSONMsg("修改失敗", false);
		}
	}
}
